<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Models\aa;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class LoginController extends Controller
{
    //注册
    public function register()
    {

        return view('index.login.register');

    }

    //注册功能
    public function register_add(Request $request)
    {

        $res= new aa();//实例化模型

        $res->name = $request->name;

        $res->pwd = md5($request->pwd);

        //获取当前时间戳存进数据库
        $data['user_time']=Date("Y-m-d H:i:s");

        $res->user_time = $data['user_time'];

        //range 是将0到100列成一个数组
        $num = range(0,9);

        //shuffle 将数组顺序随即打乱
        shuffle($num);

        //array_slice 取该数组中的某一段
        $take = array_slice($num,0,8);

        $s = implode($take);

        $res->account = '1'.$s;

        $rows = DB::table('user')->where('name',$res->name)->first();//查询表里的用户名

        if($rows!=NULL){

            return ['code'=>'-2','name'=>'用户名已存在'];

        }

        $data = $res->save();//这样就添加数据库成功了

        if($data){

            session('name',$res->name);//将name跟res传过来的name存进session


            return ['code'=>'1','name'=>'注册成功','url'=>'login_index'];//全都验证通过后走HTML里的ajax直接跳转到登录页面

        }

            return ['code'=>'-1','name'=>'注册失败'];//没有通过验证则会提醒’注册失败‘

    }

    //登录
    public function login_index()
    {

        return view('index.login.login_index');

    }

    //登录功能
    public function login_check(Request $request)
    {

        $row = $request->input();

        $rows = DB::table('user')
                ->where('name',$row['name'])
                ->where('pwd',md5($row['pwd']))
                ->first();//查询name跟pwd

        if ($rows == NULL){//判断$rows是否为空

            return ['code' => '-1', 'name' => '账号或密码错误'];

        }

        //用数组存起来
        $arr = [

            'id'   => $rows->id,
            'name' => $rows->name,
            'pwd'  => $rows->pwd,
            'img'  => $rows->img,
            'balance' => $rows->balance,
            'recharge'=> $rows->recharge,
            'account' => $rows->account

        ];
         session(['userinfo'=>$arr]);

         return ['code' => '1', 'name' => '登录成功', 'url' => '/'];

    }

    //退出登录
    public function login_out(Request $request)
    {

        $request->session('userinfo'==null)->flush();

        return ['code' => '1', 'name' => '退出成功', 'url' => '/'];

    }

    //切换账号功能：
    public function qiehuan(Request $request)
    {

        $request->session('userinfo'==null)->flush();

        echo "<script>location.href='login_index'</script>";

    }

    //头像页面
    public function img()
    {

        if(session('userinfo')['id']==""){

            return view('index.img');

        }

            $aa = session('userinfo')['id'];

            $row = DB::table('user')->where('id',$aa)->first();

            return view('index.img',['row'=>$row]);

    }

    //头像页面功能
    public function upload_img(Request $request)
    {

        $file = $request->file;//获取文件名称

        if($file -> isValid()) {

            $clientName = $file -> getClientOriginalName();//客户端文件名称

            $entension = $file -> getClientOriginalExtension();//上传文件的后缀

            $newName = md5(date('ymdhis').$clientName).".".$entension;//定义上传文件的新名称

            $path = $file -> move('touxian/article',$newName);//把缓存文件移动到制定文件夹

            $res = DB::table('user')->where('id',session('userinfo')['id'])->update(['img'=>$path]);

            if($res){

                return ['code'=>'1','name'=>'更改成功','url'=>'index_content'];

            }

                return ['code'=>'-1','name'=>'更改失败'];

        }

    }

}
